package cn.itsource.smartcampus.service.impl;

import cn.itsource.smartcampus.domain.User;
import cn.itsource.smartcampus.exception.GlobalException;
import cn.itsource.smartcampus.mapper.UserMapper;
import cn.itsource.smartcampus.service.UserService;
import cn.itsource.smartcampus.util.AjaxResult;
import cn.itsource.smartcampus.util.TokenMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public String login(User u) throws GlobalException {
        String tUname = u.getTUname();
        String tUpwd = u.getTUpwd();
        if ("".equals(tUname) || null == tUname){
            throw new GlobalException("用户名不能为空！");
        }
        if ("".equals(tUpwd) || null == tUpwd){
            throw new GlobalException("密码不能为空！");
        }
        String login = userMapper.login(u);
        if (login == null) {
            throw new GlobalException("用户名或密码不能为空！");
        }

        String token = UUID.randomUUID().toString();
        TokenMap.set(token,login);
        return token;
    }
}
